import { store } from '@/store'
import { defineStore } from 'pinia'
import * as UserApi from '@/api/member/user'

interface MemberListVO {
  // USER 缓存
  queryParams: {
    pageNo: number
    pageSize: number
    mobile: string
  }
  list: EmptyArrayType
  isSetList: boolean
  loading: boolean
  total: number
}

export const useShopMemberListStore = defineStore('shop-member-list', {
  state: (): MemberListVO => ({
    queryParams: {
      pageNo: 1,
      pageSize: 10,
      mobile: ''
    },
    list: [],
    loading: false,
    isSetList: false,
    total: 0
  }),
  getters: {
    getIsSetList(): boolean {
      return this.isSetList
    },
    getList(): EmptyArrayType {
      return this.list
    },
    getTotal(): number {
      return this.total
    },
    getLoading(): boolean {
      return this.loading
    }
  },
  actions: {
    /** 查询列表 */
    async search() {
      this.loading = true
      try {
        const data = await UserApi.getUserPage(this.queryParams)
        this.list = data.list
        this.total = data.total
        this.isSetList = true
      } finally {
        this.loading = false
      }
    },

    resetState() {
      this.queryParams = { pageNo: 1, pageSize: 10, mobile: '' }
      this.list = []
      this.isSetList = false
      this.total = 0
      this.loading = false
    }
  }
})

export const useShopMemberListStoreWithOut = () => {
  return useShopMemberListStore(store)
}
